import qs from 'qs';
import filter from './filters';
import coordtransform from 'coordtransform';
const city = {
  11: '北京',
  12: '天津',
  13: '河北',
  14: '山西',
  15: '内蒙古',
  21: '辽宁',
  22: '吉林',
  23: '黑龙江',
  31: '上海',
  32: '江苏',
  33: '浙江',
  34: '安徽',
  35: '福建',
  36: '江西',
  37: '山东',
  41: '河南',
  42: '湖北',
  43: '湖南',
  44: '广东',
  45: '广西',
  46: '海南',
  50: '重庆',
  51: '四川',
  52: '贵州',
  53: '云南',
  54: '西藏',
  61: '陕西',
  62: '甘肃',
  63: '青海',
  64: '宁夏',
  65: '新疆',
  71: '台湾',
  81: '香港',
  82: '澳门',
  91: '国外 '
};
/* eslint-disable */
Date.prototype.Format = function (fmt) {
  var o = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      "S": this.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  }
  return fmt;
};
/* eslint-enable */
function identityCodeValid (code) {
  code && (code = code.toUpperCase());
  let pass = true;
  if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/i.test(code)) {
    // 简单正则
    pass = false;
  } else if (!city[code.substr(0, 2)]) {
    // 地址编码错误
    pass = false;
  } else if (!birthDayValidate(code)) {
    pass = false;
  } else {
    // 18位身份证需要验证最后一位校验位
    if (code.length == 18) {
      code = code.split('');

      // ∑(ai×Wi)(mod 11)
      // 加权因子
      const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];

      // 校验位
      const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
      let sum = 0;
      let ai = 0;
      let wi = 0;
      for (let i = 0; i < 17; i++) {
        ai = code[i];
        wi = factor[i];
        sum += ai * wi;
      }
      if (parity[sum % 11] != code[17]) {
        // 校验位错误
        pass = false;
      }
    }
  }
  return pass;
}
function birthDayValidate (idCard18) {
  const year = idCard18.substring(6, 10);
  const month = idCard18.substring(10, 12);
  const day = idCard18.substring(12, 14);
  const tempDate = new Date(year, parseFloat(month) - 1, parseFloat(day));
  if (tempDate.getFullYear() != parseFloat(year) || tempDate.getMonth() != parseFloat(month) - 1 || tempDate.getDate() != parseFloat(day)) {
    return false;
  }
  return true;
}

export default {
  stringify: qs.stringify,
  productImg: filter.productImg,
  productImg2: filter.productImg2,
  mobileValidator: (value) => {
    if (value == '') {
      return '请输入手机号码';
    } else if (/^1[34578]\d{9}$/.test(value)) {
      return '';
    } else {
      return '请输入正确的手机号码';
    }
  },
  requireValidator: (value, name, tip) => {
    // if (value instanceof Array) {
    //   if (value.length == 0) {
    //     return '请' + (tip || '输入或选择') + name;
    //   }
    // }
    if (!value) {
      return '请' + (tip || '输入或选择') + name;
    } else {
      return '';
    }
  },
  identityCodeValid: identityCodeValid,
  getTodayStr: () => {
    var today = new Date();
    return today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
  },
  toggleAttr: function (arr, index, attrName = 'active') {
    arr.forEach(element => {
        element[attrName] = false;
    });
    arr[index] && (arr[index][attrName] = true);
  },
  find: function (v, list, id = 'id', name = 'name') {
    var obj = list.find((n) => n[id] == v);
    return (obj && obj[name]) || '--';
  },
  findItem: function (v, list, id = 'id') {
    var obj = list.find((n) => n[id] == v);
    return obj || {};
  },
  tenFmt: function (val) {
    // 1 return 01
    return val < 10 ? '0' + val : val;
  },
  bd2wgs: function (lng, lat) {
    var gcj = coordtransform.bd09togcj02(lng, lat);
    return coordtransform.gcj02towgs84(gcj[0], gcj[1]);
  },
  wgs2bd: function (lng, lat) {
    var gcj = coordtransform.wgs84togcj02(lng, lat);
    return coordtransform.gcj02tobd09(gcj[0], gcj[1]);
  },
  getSjly: function () {
    return 'LX';
  },
  getUseTime: function (startDate, endDate = new Date()) {
    var ctime = parseInt((new Date(endDate).getTime() - new Date(startDate).getTime()) / 1000);
    // debugger
    let returnStr = '';

    // 小时位
    let hr = Math.floor(ctime / 3600);
    let hrStr = hr.toString();
    hrStr != '0' && (returnStr = hrStr + '时');
    // 分钟位
    let min = Math.floor(ctime / 60 % 60);
    let minStr = min.toString();
    minStr != '0' && (returnStr += minStr + '分');
    // 秒位
    let sec = Math.floor(ctime % 60);
    let secStr = sec.toString();
     returnStr += secStr + '秒';
    return returnStr;
  }
};
